import React from 'react'
import { Panel, Button } from 'react-weui'
import { browserHistory } from 'react-router'

export default class ShoppingCartBar extends React.Component {
	constructor(props) {
        super(props)
        this.state = {
            totalPrice: 0.00,
            disabled:null
        }
    }
	componentDidMount(){
	}
    componentWillReceiveProps(nextProps) {
        if(nextProps.shoppingCart.shoppingCart.isFetched == true) {
        	var price=0;
        	var array = nextProps.goodsData.map(function(item, i) {
                return parseFloat(item.goodsPrice*item.goodsNum)
            });
        	for(var i=0;i<array.length;i++){
        		price+=array[i];
        	}
            var cartIds = nextProps.goodsData.map(function(item,i){
                return item.cartId
            });
        	this.setState({
                totalPrice: price,
                disabled:false,
                cartIds: cartIds
            })
        }
        if(nextProps.shoppingCart.shoppingCart.isFetched == false){
        	this.setState({
                totalPrice: 0.00,
                disabled:true
            })
        }
    }
	handleOrderPreview() {
		const storeId = this.props.storeId;
        browserHistory.push(`/store/${storeId}/user/order-preview`);
        this.props.previewOrder(this.state.cartIds)
	}

	render() {
		return (
			<Panel style={{
				position: 'fixed',
				bottom: '0',
				width: '100%',
				textAlign: 'center',
				padding: '18px 0 18px 0',
				borderTop: '1px solid #ccc'
			}}>
                 <p>支付总额：<span style={{ color: 'orange' }}>￥{this.state.totalPrice}</span></p>
                 <Button type={this.props.isCart?'warn':'primary'} style={{
                 	width: '90%',
                 	marginTop: '10px'
                 }} onClick={this.handleOrderPreview.bind(this)}
                 disabled={this.state.disabled}>{this.props.btn}</Button>
			</Panel>
		);
	}
}
